package models;

import java.util.List;

import siena.Column;
import siena.Generator;
import siena.Id;
import siena.Model;
import siena.Query;
import siena.Table;

/**
 * 账户
 * @author 高恒
 *
 */
@Table("account")
public class Account extends Model {

	@Id(Generator.AUTO_INCREMENT)
	public Long id;
	
	/**
	 * 所有人id
	 */
	@Column("owner_id")
	public Long ownerId;
	
	/**
	 * 名称
	 */
	@Column("name")
	public String name;
	
	/**
	 * 描述
	 */
	@Column("description")
	public String description;
	
	/**
	 * 类型
	 */
	@Column("type")
	public AccountType type;
	
	/**
	 * 余额
	 */
	@Column("balance")
	public Double balance;
	
	public Account(Long ownerId, String name, String description) {
		this(ownerId, name, description, AccountType.getDefault());
	}
	
	public Account(Long ownerId, String name, String description,
			AccountType type) {
		super();
		this.ownerId = ownerId;
		this.name = name;
		this.description = description;
		this.type = type;
	}

	public List<AccountItem> getItems() {
		return AccountItem.all().filter("accountId", this.id).fetch();
	}

	public static Account get(Long id) {
		return all().filter("id", id).get();
	}
	
	public static List<Account> getUserAccounts(Long userId) {
		return all().filter("ownerId", userId).fetch();
	}

	public static Query<Account> all() {
		return Model.all(Account.class);
	}
	
}
